package com.jeff.service;

import com.jeff.entity.OrderEntity;
import com.jeff.feign.StorageFeignClient;
import com.jeff.repository.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author yangjf
 * @ClassName OrderService
 * @Description: TODO
 * @date 2021/8/19 17:57
 */
@Service
public class OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageFeignClient storageFeignClient;

    /**
     * 下单：创建订单、减库存，涉及到两个服务
     *
     * @param userId
     * @param commodityCode
     * @param count
     */
    @GlobalTransactional
    public void placeOrder(String userId, String commodityCode, Integer count) {
        BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setUserId(userId);
        orderEntity.setCommodityCode(commodityCode);
        orderEntity.setCount(count);
        orderEntity.setMoney(orderMoney);
        orderMapper.insert(orderEntity);
        storageFeignClient.deduct(commodityCode, count);
    }
}
